package com.heima.behavior.controller;

import com.heima.behavior.dto.BehaviorDto;
import com.heima.common.dto.ResponseResult;
import com.heima.common.dto.User;
import com.heima.common.enums.AppHttpCodeEnum;
import com.heima.common.util.UserThreadLocalUtil;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/api/v1/behavior")
public class BehaviorController {
    @Autowired
    private StringRedisTemplate redisTemplate;
    @PostMapping("/load_article_behavior")
    public ResponseResult loadArticleBehavior(@RequestBody BehaviorDto dto){
        User user = UserThreadLocalUtil.get();
        if(user==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }
        dto.setUserId(user.getUserId());
        Map<String,Boolean> map = new HashMap<>();
        map.put("isfollow",redisTemplate.hasKey("saveFollowBehavior" +dto.getUserId()+"_"+dto.getEquipmentId()+"_"+dto.getAuthorId() +"_"+dto.getArticleId()));
        map.put("islike",redisTemplate.hasKey("saveLikeBehavior" +dto.getUserId()+"_"+dto.getEquipmentId()+"_null_"+dto.getArticleId()));
        map.put("isunlike",redisTemplate.hasKey("saveUnlikesBehavior" +dto.getUserId()+"_"+dto.getEquipmentId()+"_null_"+dto.getArticleId()));
        map.put("iscollection",redisTemplate.hasKey("saveCollectionBehavior" +dto.getUserId()+"_"+dto.getEquipmentId()+"_null_"+dto.getArticleId()));
        return ResponseResult.okResult(map);
    }
}
